package com.mediacloud.app.newsmodule.widget

import android.graphics.Color
import android.view.View
import com.hebei.adapter.WheelAdapter
import com.mediacloud.app.model.fragment.LBottomSheetFragment
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import kotlinx.android.synthetic.main.micro_live_wheel_view_layout.*
import java.lang.Math.min
import java.util.*
import kotlin.collections.ArrayList


class DateWheelView2 : LBottomSheetFragment() {
    val c = Calendar.getInstance()
    val list1: MutableList<DateWheelMod> = ArrayList()
    val list2: MutableList<DateWheelMod> = ArrayList()
    val list3: MutableList<DateWheelMod> = ArrayList()
    var listener: OnSelectedListener? = null
    var year = 0
    var month = 0
    var day = 0


    override fun draggable() = false
    override fun onViewCreated(mRootView: View) {


        c.time = Date(System.currentTimeMillis())
        year = c.get(Calendar.YEAR)
        month = c.get(Calendar.MONTH)
        day = c.get(Calendar.DAY_OF_MONTH)

        arguments?.getString("TITLE")?.let {
            titleView.text = it
        }


        val year = c.get(Calendar.YEAR)
        val min = year - 10
        for (i in 0..10) {
            val y = min + i
            list1.add(DateWheelMod("${y}年", y))
        }
        for (i in 1..month + 1) {
            list2.add(DateWheelMod("${i}月", i))
        }

        val days = c.get(Calendar.DAY_OF_MONTH)

        for (i in 1..days) {
            list3.add(DateWheelMod("${i}日", i))
        }





        options1.setLineSpacingMultiplier(3.2f)
        options2.setLineSpacingMultiplier(3.2f)
        options3.setLineSpacingMultiplier(3.2f)

        options1.setTextSize(17f)
        options2.setTextSize(17f)
        options3.setTextSize(17f)
        val color = AppFactoryGlobalConfig.getAppServerConfigInfo(context).mainColor
        options1.setTextColorCenter(Color.BLACK)
        options2.setTextColorCenter(Color.BLACK)
        options3.setTextColorCenter(Color.BLACK)
        submit.setTextColor(color)

        options1.setTextColorOut(Color.BLACK)
        options2.setTextColorOut(Color.BLACK)
        options3.setTextColorOut(Color.BLACK)


        options3.setDividerColor(Color.parseColor("#DDDDDD"))
        options1.setCyclic(false)
        options2.setCyclic(false)
        options3.setCyclic(false)
        options1.adapter = object : WheelAdapter<Any?> {
            override fun getItemsCount(): Int {
                return list1.size
            }

            override fun getItem(index: Int): Any? {
                return list1.get(index)
            }

            override fun indexOf(o: Any?): Int {
                return list1.indexOf(o)
            }
        }


        options2.adapter = object : WheelAdapter<Any?> {
            override fun getItemsCount(): Int {
                return list2.size
            }

            override fun getItem(index: Int): Any? {
                return list2.get(index)
            }

            override fun indexOf(o: Any?): Int {
                return list2.indexOf(o)
            }
        }

        options3.adapter = object : WheelAdapter<Any?> {
            override fun getItemsCount(): Int {
                return list3.size
            }

            override fun getItem(index: Int): Any? {
                return list3.get(index)
            }

            override fun indexOf(o: Any?): Int {
                return list3.indexOf(o)
            }
        }

        options1.setOnItemSelectedListener {

            c.set(Calendar.YEAR, list1[it].value as Int)

            aaa()

        }


        options2.setOnItemSelectedListener {
            c.set(Calendar.MONTH, it)
            aaa()

        }


        cancel.setOnClickListener {
            dismiss()
        }
        submit.setOnClickListener {
//            val time = list1[options1.currentItem].value + list2[options2.currentItem].value + list3[options3.currentItem].value
            listener?.onSelected(0)
            dismiss()
        }


        options1.currentItem = list1.size - 1
        options2.currentItem = c.get(Calendar.MONTH)
        options3.currentItem = c.get(Calendar.DAY_OF_MONTH) - 1


        arguments?.getLong("TIME_STAMP")?.let {
            c.time = Date(it)
            aaa()
            val year = c.get(Calendar.YEAR)
            for (i in 0 until list1.size) {
                val d = list1[i]
                if (d.value == year) {
                    options1.currentItem = i
                    break
                }
            }
            options2.currentItem = c.get(Calendar.MONTH)
            options3.currentItem = c.get(Calendar.DAY_OF_MONTH) - 1
        }
        c.time = Date(System.currentTimeMillis())

    }

    private fun aaa() {
        if (c.get(Calendar.YEAR) < this.year) {
            list2.clear()
            for (i in 1..12) {
                list2.add(DateWheelMod("${i}月", i))
            }
            list3.clear()
            val days = c.getActualMaximum(Calendar.DAY_OF_MONTH)
            for (i in 1..days) {
                list3.add(DateWheelMod("${i}日", i))
            }
            options3.postInvalidate()

        } else {
            list2.clear()
            for (i in 1..month + 1) {
                list2.add(DateWheelMod("${i}月", i))
            }
            list3.clear()
            if (c.get(Calendar.MONTH) < this.month) {
                val days = c.getActualMaximum(Calendar.DAY_OF_MONTH)
                for (i in 1..days) {
                    list3.add(DateWheelMod("${i}日", i))
                }
            } else {
                val days = c.get(Calendar.DAY_OF_MONTH)
                for (i in 1..days) {
                    list3.add(DateWheelMod("${i}日", i))
                }
            }
            options3.postInvalidate()
        }
    }

    override fun onLayoutRes() = R.layout.micro_live_wheel_view_layout

    interface OnSelectedListener {
        fun onSelected(timeStamp: Long)
    }
}